package com.demo.rabbit.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MessageListener {

    public static final String EXCHANGE_NAME = "exchange.test.priority";
    public static final String QUEUE_NAME = "queue.test.priority";
    public static final String ROUTING_KEY = "routing.test.priority";


    /*
      优先级队列
      问题：在消费端启动时通过 @RabbitListener 注解自动创建优先级队列，在 @Queue 注解中 增加 arguments 参数创建队列失败，导致项目无法正常启动。
      解决：可以在生产者端项目启动时创建优先级队列 或者 通过 rabbitmq 管控台进行创建。
           消费端代码直接监听队列即可（@RabbitListener(queues = QUEUE_NAME)）
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_NAME, durable = "true", arguments = @Argument(name = "x-max-priority", value = "10", type = "java.lang.Number")),
            exchange = @Exchange(value = EXCHANGE_NAME, durable = "true"),
            key = {ROUTING_KEY}
        )
    )
    */
    @RabbitListener(queues = QUEUE_NAME)
    public void processMessage(String data, Message message, Channel channel) throws Exception{
        log.info("优先级队列 - 消息消费端，接收到的消息：{}", data);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
